package org.dragonnova.business.modules.sys.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.dragonnova.business.model.Pager;
import org.dragonnova.business.model.User;
import org.dragonnova.business.modules.sys.service.impl.UserServiceImpl;
import org.dragonnova.business.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.pub.exception.BusinessException;

/**
 *  @description: 
 *  @author songxy  DateTime 2017年6月8日 下午4:04:55 
 *  @company winter
 *  @email thinkdata@163.com  
 *  @version 1.0
 */

/**
 * @description:
 * @author songxy DateTime 2017年6月8日 下午4:04:56
 * @company winter
 * @email thinkdata@163.com
 * @version 1.0
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
	@Autowired
	private UserServiceImpl userService;

	/**
	 * @description:
	 * @author songxy DateTime 2017年6月8日 下午4:04:46
	 * @param session
	 * @param id
	 * @param searchText
	 * @param page
	 * @param pageSize
	 * @return
	 * @throws BusinessException
	 */
	@RequestMapping("/findUser")
	@ResponseBody
	public Map<String, Object> findUser(HttpSession session, Integer id,
			String searchText,
			@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "pageSize", defaultValue = "15") int pageSize)
			throws BusinessException {
		Map<String, Object> map = new HashMap<>();
		map.put("id", id);
		map.put("searchText", searchText);
		Pager pager = new Pager(pageSize, page);
		map.put("page", pager);
		try {
			Map<String, Object> resultMap = userService.findUser(map);
			resultMap.put("page", pager);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException("300", e.getMessage());
		}
	}

	/**
	 * update时id必须
	 */
	@RequestMapping("/saveOrUpdateUser")
	@ResponseBody
	public Map<String, Object> saveOrUpdateUser(HttpSession session, User user)
			throws BusinessException {
		try {
			String pwd = user.getPassword();
			if (pwd != null) {
				user.setPassword(DigestUtils.md5DigestAsHex(pwd.getBytes()));
			}
			User u = userService.addUser(user);
			if (u != null) {
				u.setPassword("");
				return packData(u);
			} else {
				return packInfo(false, "保存");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException(e.getCause());
		}
	}

	/**
	 * 修改密码
	 */
	@RequestMapping("/updatePassword")
	@ResponseBody
	public Map<String, Object> updatePassword(HttpSession session,
			@RequestParam(value = "id", required = true) Integer id,
			@RequestParam(value = "oldPwd", required = true) String oldPwd,
			@RequestParam(value = "newPwd", required = true) String newPwd)
			throws BusinessException {
		try {
			return userService.updatePassword(id, oldPwd, newPwd);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException(e.getMessage(), e.getCause());
		}
	}

	/**
	 * 删除用户
	 */
	@RequestMapping("/delUser")
	@ResponseBody
	public Map<String, Object> delUser(HttpSession session, Integer id)
			throws BusinessException {
		if (id == null) {
			return packInfo(false, "id为空，");
		}
		try {
			return userService.delUser(id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException(e.getMessage(), e.getCause());
		}
	}
}
